{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#Keras-MNIST-Example\" data-toc-modified-id=\"Keras-MNIST-Example-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Keras MNIST Example</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Keras <a class=\"tocSkip\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Keras MNIST Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_1 (Dense)              (None, 128)               100480    \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 128)               16512     \n",
      "_________________________________________________________________\n",
      "dropout_2 (Dropout)          (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 118,282\n",
      "Trainable params: 118,282\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "Epoch 1/10\n",
      "60000/60000 [==============================] - 3s 45us/step - loss: 2.3189 - acc: 0.1149\n",
      "Epoch 2/10\n",
      "60000/60000 [==============================] - 2s 35us/step - loss: 2.2513 - acc: 0.1761\n",
      "Epoch 3/10\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 2.1656 - acc: 0.2686\n",
      "Epoch 4/10\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 2.0350 - acc: 0.3792\n",
      "Epoch 5/10\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 1.8411 - acc: 0.4734\n",
      "Epoch 6/10\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 1.6026 - acc: 0.5484: 0s - loss: 1.64\n",
      "Epoch 7/10\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 1.3812 - acc: 0.6058\n",
      "Epoch 8/10\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 1.2057 - acc: 0.6496\n",
      "Epoch 9/10\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 1.0779 - acc: 0.6844\n",
      "Epoch 10/10\n",
      "60000/60000 [==============================] - 2s 25us/step - loss: 0.9752 - acc: 0.7150\n",
      "10000/10000 [==============================] - 0s 34us/step\n",
      "\n",
      " loss: 0.855843718529\n",
      "\n",
      " accuracy: 0.7909\n"
     ]
    }
   ],
   "source": [
    "# import the keras modules\n",
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout\n",
    "from keras.optimizers import SGD\n",
    "from keras import utils\n",
    "import numpy as np\n",
    "\n",
    "# define some hyper parameters\n",
    "batch_size = 100\n",
    "n_inputs = 784\n",
    "n_classes = 10\n",
    "n_epochs = 10\n",
    "\n",
    "# get the data\n",
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "# reshape the two dimensional 28 x 28 pixels\n",
    "#   sized images into a single vector of 784 pixels\n",
    "x_train = x_train.reshape(60000, n_inputs)\n",
    "x_test = x_test.reshape(10000, n_inputs)\n",
    "\n",
    "# convert the input values to float32\n",
    "x_train = x_train.astype(np.float32)\n",
    "x_test = x_test.astype(np.float32)\n",
    "\n",
    "# normalize the values of image vectors to fit under 1\n",
    "x_train /= 255\n",
    "x_test /= 255\n",
    "\n",
    "# convert output data into one hot encoded format\n",
    "y_train = utils.to_categorical(y_train, n_classes)\n",
    "y_test = utils.to_categorical(y_test, n_classes)\n",
    "\n",
    "# build a sequential model\n",
    "model = Sequential()\n",
    "# the first layer has to specify the dimensions of the input vector\n",
    "model.add(Dense(units=128, activation='sigmoid', input_shape=(n_inputs,)))\n",
    "# add dropout layer for preventing overfitting\n",
    "model.add(Dropout(0.1))\n",
    "model.add(Dense(units=128, activation='sigmoid'))\n",
    "model.add(Dropout(0.1))\n",
    "# output layer can only have the neurons equal to the number of outputs\n",
    "model.add(Dense(units=n_classes, activation='softmax'))\n",
    "\n",
    "# print the summary of our model\n",
    "model.summary()\n",
    "\n",
    "# compile the model\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=SGD(),\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "# train the model\n",
    "model.fit(x_train, y_train,\n",
    "          batch_size=batch_size,\n",
    "          epochs=n_epochs)\n",
    "\n",
    "# evaluate the model and print the accuracy score\n",
    "scores = model.evaluate(x_test, y_test)\n",
    "\n",
    "print('\\n loss:', scores[0])\n",
    "print('\\n accuracy:', scores[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
